// 金额通道
import Vue from 'vue'

const moneyFilter = function (s, type) {
  let a = 0
  if (/[\~!-]/g.test(s)) {
    a = s.toString().replace('-', '')
  } else {
    a = s
  }
  if (/[^0-9\.]/.test(a)) {
    return '0.00'
  }
  if (a == null || a == 'null' || a == '') {
    return '0.00'
  }
  a = a.toString().replace(/^(\d*)$/, '$1.')
  a = (a + '00').replace(/(\d*\.\d\d)\d*/, '$1')
  a = a.replace('.', ',')
  let re = /(\d)(\d{3},)/
  while (re.test(a)) {
    a = a.replace(re, '$1,$2')
  }
  a = a.replace(/,(\d\d)$/, '.$1')
  if (type == 0) {
    let b = a.split('.')
    if (b[1] == '00') {
      a = b[0]
    }
  }
  if (/[\~!-]/g.test(s)) {
    return '¥' + '-' + a
  } else {
    return '¥' + a
  }
}

export default {
  install: function () {
    Vue.filter('money', moneyFilter)
  }
}
